package com.sfac.javaSpringBoot.modules.test.controller;

import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import com.sfac.javaSpringBoot.modules.test.entity.Student;
import com.sfac.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {

    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }


    //    127.0.0.1/api/student/
//    {"name":"hieheiehi","email":"hhh@qq.com","age":"20"}
//    {"name":"hieheiehi","email":"hhh@qq.com","age":"20","studentCard":{"SFAC_011111"}}
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":1, "name":"hujiangggg","email":"hjjjj@163.com","age":18}
     */
//    更新数据
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    //    删除单个数据
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> DeleteStudentById(@PathVariable int id) {
        return studentService.DeleteStudentById(id);
    }

    //    查询单个数据
    @GetMapping("/student/{id}")
    public Student getOneById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    //    查询全部
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    //多条件查询
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name, email);
    }

    //    127.0.0.1/api/students
//{"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyword":""}
//    多条件分页查询
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }
//    127.0.0.1/api/students/v2
    @PostMapping(value = "/students/v2",consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<Student> students){
        return studentService.insertStudents(students);
    }


}
